<?php
/**
 * Created by PhpStorm.
 * User: Hong
 * Date: 2018/3/24
 * Time: 16:06
 * Function:
 */

namespace Organize\Controllers\Leaders;

use Organize\Policies\LeaderPolicy;
use Illuminate\Http\Request;
use Organize\Controllers\BaseOrganizeController;
use Organize\Resources\LeaderResources;
use Platform\Models\Organize\Leader;
use Platform\Models\Organize\LeaderPermission;
use Platform\Models\Organize\LeaderRole;
use Platform\Repository\Organize\LeaderRepository;
use Platform\System\Tools\ResultTool;


/**
 * @module 组织成员管理
 * Class UserController
 * @package App\Http\Controllers\Admin\System
 */
class LeaderController extends BaseOrganizeController
{
    protected $repository;
    protected $policy;

    /**
     * UserController constructor.
     * @param LeaderRepository $repository
     * @param LeaderPolicy $policy
     */
    public function __construct(LeaderRepository $repository, LeaderPolicy $policy)
    {
        parent::__construct();
        $this->repository = $repository;
        $this->policy = $policy;
    }

    /**
     * @permission 成员列表
     * 当前登陆用户所属组织下的成员列表
     * @param Request $request
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function index(Request $request){

        $organize = $this->leader->organize;
        $leaders = $this->repository->screen($request,$organize);
        return myResult()->setData($leaders,LeaderResources::class)->setPage($leaders)->send();

    }

    /**
     * @permission 创建成员
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(Request $request)
    {
        $data = $this->verify($request);
        Leader::create($data);

        return myResult()->setCode(ResultTool::CODE_SUCCESS,'新增成功')->send();
    }

    /**
     * @permission 查看成员信息
     * @param Leader $leader
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show(Leader $leader)
    {
        $this->policy->show($this->leader,$leader);
        $leader->load(['role','organize']);
        return myResult()->setData($leader,LeaderResources::class)->send();
    }


    /**
     * @permission 查看自身权限信息
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showSelfPermissions()
    {
        $permissions = LeaderPermission::get(['id','name','module','uri','method']);
        $my_permissions = $this->admin->allPermissions();

        $data = LeaderPermission::makeData($permissions,$my_permissions);
        return myResult()->setData($data)->send();
    }

    /**
     * @permission 修改成员信息
     * @param Leader $leader
     * @param Request $request
     * @return ResultTool
     */
    public function update(Leader $leader, Request $request)
    {
        $this->policy->update($this->leader,$leader);

        $data = $this->verify($request,$leader);

        $leader->update($data);

        return myResult()->setCode(ResultTool::CODE_SUCCESS,'编辑成功')->send();
    }

    /**
     * @permission 删除成员
     * @param Leader $leader
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(Leader $leader)
    {
        $this->policy->destroy($this->leader,$leader);
        $leader->delete();
        return myResult()->setCode(ResultTool::CODE_SUCCESS,'删除成功')->send();
    }

    /**
     * 数据验证
     * @param Request $request
     * @param Leader|null $leader
     * @return array
     */
    public function verify(Request $request,Leader $leader = null){

        $data = $request->only(['username','password','name','avatar','role']);

        if ($leader) {

            if (isset($data['username'])){
                $admin_log = $this->repository->getOneByUsernameExpectThis($data['username'],$leader->id);
                if ($admin_log)
                    myResult()->stop(ResultTool::CODE_TARGET_WRONG,'用户名已被使用');
            }

            if ($leader->isOrganizeAdmin()){
                unset($data['role']);
            }

        }else{

            if (empty($data['username']))
                myResult()->stop(ResultTool::CODE_PARAM_LOST, '用户名不可为空');

            if (getStrLength($data['username']) > 20)
                myResult()->stop(ResultTool::CODE_PARAM_WRONG,'用户名过长');

            $admin_log = $this->repository->getOneByUsername($data['username']);
            if ($admin_log)
                myResult()->stop(ResultTool::CODE_TARGET_WRONG,'用户名已被使用');

            if (empty($data['password']))
                myResult()->stop(ResultTool::CODE_PARAM_LOST,'密码不可为空');

            $data['password'] = bcrypt($data['password']);

            if (empty($data['name']))
                myResult()->stop(ResultTool::CODE_PARAM_LOST,'昵称不可为空');

            if (getStrLength($data['name']) > 20)
                myResult()->stop(ResultTool::CODE_PARAM_WRONG,'昵称过长');

            if (getStrLength($data['name']) > 20)
                myResult()->stop(ResultTool::CODE_PARAM_WRONG,'昵称过长');

            $data['organize_id'] = $this->organize->id;
        }

        if (isset($data['role'])){
            $data['role_id'] = null;
            $role_log = $this->organize->role()->where('id',$data['role'])->first();
            if ($role_log) {
                $data['role_id'] = $data['role'];
            }
            unset($data['role']);
        }

        return $data;
    }
}